<template>
    <el-row :gutter="10">
  <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
    <div class="grid-content bg-purple">
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
  <el-form-item
    prop="topic"
    label="调研题目："
    :rules="[
      { required: true, message: '题目不能为空', trigger: 'blur' }
    ]"
  >
    <el-input v-model="dynamicValidateForm.topic"></el-input>
  </el-form-item>
  <el-form-item
    v-for="(domain, index) in dynamicValidateForm.domains"
    :label="'答案:' + index"
    :key="domain.key"
    :prop="'domains.' + index + '.ansers'"
    :rules="{
      required: true, message: '答案不能为空', trigger: 'blur'
    }"
  >
    <el-input v-model="domain.ansers"></el-input>
    <el-button @click.prevent="removeDomain(domain)">删除</el-button>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
    <el-button @click="addDomain">新增答案</el-button>
    <!-- <el-button @click="addDomains">新增题目</el-button> -->
    <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>

  </el-form-item>
</el-form>
  </div>
</el-col>
</el-row>
</template>

<script>
import {addtemplate} from '@/api/templateTable'
  export default {
    data() {
      return {
        dynamicValidateForm: {
          domains: [{
            ansers: ""
          }],
          topic: ''
        }
      };
    },
    methods: {
      submitForm(formName) {
          console.log(this.dynamicValidateForm);
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // console.log(this.dynamicValidateForm.topic);
            // //console.log(this.dynamicValidateForm.domains);
            // console.log(this.dynamicValidateForm.domains[1].ansers);
            addtemplate(
            {
              answers:this.dynamicValidateForm.domains,
              topic:this.dynamicValidateForm.topic
            }
            ).
            then(res =>{
            console.log(res);
             if (res.code==200) {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.data.message
                })
              }
            }).catch()
            // alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      removeDomain(item) {
        var index = this.dynamicValidateForm.domains.indexOf(item)
        if (index !== -1) {
          this.dynamicValidateForm.domains.splice(index, 1)
        }
      },
      addDomain() {
        this.dynamicValidateForm.domains.push({
          value: '',
          key: Date.now()
        });
      },
      // addDomains() {
      //   this.dynamicValidateForms.domains.push({
      //     value: '',
      //     key: Date.now()
      //   });
      // }
    }
  }
</script>

<style>
  .el-col {
    border-radius: 9px;
  }
 
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
</style>